<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/7/10
 * Time: 14:44
 */

namespace User\Controller;


use Think\Controller;

class FileController extends Controller
{

    public function upload(){

        $msg = array(
            'code' => 0,
            'msg'=>'上传成功',
        );

        if($_GET['old_img']){
            unlink($_GET['old_img']);
        }

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './data/'; // 设置附件上传根目录
        $upload->savePath  =     'upload/store/'; // 设置附件上传（子）目录
        // 上传文件

        if(!is_dir($upload->rootPath.$upload->savePath)) mkdir($upload->rootPath.$upload->savePath,0777,true);

        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $msg['code'] = 1001;
            $msg['msg'] = $upload->getError();
        }else{// 上传成功

            $res = $info['Filedata'];
            $msg['url'] = '/data/'.$res['savepath'].$res['savename'];
        }
        $this->ajaxReturn($msg);
    }


    // 多图上传
    public function uploads(){

        $msg = array(
            'code' => 0,
            'msg'=>'上传成功',
        );

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './data/'; // 设置附件上传根目录
        $upload->savePath  =     'upload/store/'; // 设置附件上传（子）目录
        // 上传文件

        if(!is_dir($upload->rootPath.$upload->savePath)) mkdir($upload->rootPath.$upload->savePath,0777,true);

        $info   =   $upload->upload();
        if(!$info) {  // 上传错误提示错误信息
            $msg['code'] = 1001;
            $msg['msg'] = $upload->getError();
        }else{     // 上传成功

            $res = $info['Filedata'];
            $res['path'] = '/data/'.$res['savepath'].$res['savename'];
            $msg['pic_id'] = D("Picture")->add($res);
            $msg['url'] = '/data/'.$res['savepath'].$res['savename'];
        }
        $this->ajaxReturn($msg);
    }

    public function delete(){
        $info['code'] = 0;
        $info['msg'] = "删除失败!";
        if(IS_POST){
            $file = I("post.path",'');
            $id = I('post.id',0,'intval');
            unlink('.'.$file);
            D("ProSlider")->where(array('id'=>$id))->delete();
            $info['code'] = 1;
            $info['msg'] = "删除成功";
        }
        $this->ajaxReturn($info);
    }

}